<?php
class admin_controller_content_category extends admin_controller
{

    public function listing()
    {
        $model_category = $this->get_admin_model('category');

        $template = $this->get_admin_template();
        $template->set_title('分类管理');
        $template->set('categories', $model_category->get_categories());
        $template->display();
    }

    public function save()
    {
        $ids = post('id', array(), 'array(int)');
        $parent_ids = post('parent_id', array(), 'array(int)');
        $names = post('name', array(), 'array(string)');
        
        if (count($ids))
        {
            for ($i = 0, $n = count($ids); $i < $n; $i++)
            {
                if (!($ids[$i] || $names[$i])) continue;
                
                $table_category = $this->get_table('category');
                $table_category->id = $ids[$i];
                $table_category->parent_id = $parent_ids[$i];
                $table_category->name = $names[$i];
                $table_category->ordering = $i;
                $table_category->save();
            }
        }
        
        system_log('修改文章分类信息');

        $this->redirect('./?action=content.category.listing', '保存分类成功');
    }

    public function delete()
    {
        $id = get('id', 0, 'int');
        if (!$id)
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '参数{category_id}丢失');
        }
        else
        {
            $table_category = $this->get_table();
            $table_category->load($id);
            
            $model = $this->get_admin_model();
            if ($model->delete($id))
            {
                $this->set_ajax('status', 0);
                $this->set_ajax('description', '删除成功');
                
                system_log('删除文章分类 #'.$id.' '.$table_category->name);
            }
            else
            {
                $this->set_ajax('status', 2);
                $this->set_ajax('description', $model->get_error());
            }
        }
        $this->ajax();
    }
    

}
?>